package com.finger.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.commonutils.R;
import com.finger.eduservice.entity.EduTeacher;
import com.finger.eduservice.entity.vo.QueryTeacher;
import com.finger.eduservice.exception.GuliException;
import com.finger.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-21
 */
@Log4j2
@Api(description = "讲师接口")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查找全部教师
     * @return 教师列表
     */
    @ApiOperation(value = "查找全部教师")
    @GetMapping("/findALl")
    public R findAll() {

        try {
            List<EduTeacher> list = eduTeacherService.list(null);
            return R.ok().data("allTeacher", list);
        } catch (Exception e) {
            throw new GuliException(500, "出现异常");
        }

    }

    /**
     * 根据教师ID删除教师
     * @param id 教师ID
     * @return
     */
    @ApiOperation(value = "删除教师")
    @DeleteMapping("/{id}")
    public R remove(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id) {
        try {
            boolean result = eduTeacherService.removeById(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 教师分页
     * @param page  当前页码
     * @param size  页面大小
     * @return
     */
    @ApiOperation(value = "教师分页")
    @GetMapping("/{page}/{size}")
    public R pageList(@ApiParam(name = "page", value = "当前页码", required = true, defaultValue = "1") @PathVariable("page") Long page,
                      @ApiParam(name = "size", value = "页面大小", required = true, defaultValue = "3") @PathVariable("size") Long size) {
        try {
            Page<EduTeacher> pages = new Page<>(page, size);

            eduTeacherService.page(pages, null);

            return R.ok().data("page", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 添加教师
     * @param teacher 教师对象
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping
    public R save(@ApiParam(name = "teacher", value = "讲师对象", required = true)
                  @RequestBody EduTeacher teacher) {

        boolean saveSuccess = eduTeacherService.save(teacher);

        if (saveSuccess) {
            return R.ok().success(saveSuccess);
        } else {
            return R.error();
        }
    }

    /**
     * 根据ID查询讲师
     * @param id 教师ID
     * @return
     */
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/{id}")
    public R getTeacherById(@ApiParam(name = "id", required = true, value = "讲师ID")
                            @PathVariable("id") String id) {
        EduTeacher teacher = eduTeacherService.getById(id);

        if (!Objects.isNull(eduTeacherService)) {
            return R.ok().data("teacher", teacher);
        } else {
            return R.error();
        }
    }

    /**
     * 根据ID更新讲师数据
     * @param teacher 教师对象
     * @return
     */
    @ApiOperation(value = "根据ID更新讲师数据")
    @PutMapping
    public R updateTeacherById(@ApiParam(name = "teacher", value = "讲师对象", required = false)
                               @RequestBody EduTeacher teacher){

        try {
            eduTeacherService.updateById(teacher);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 根据条件查询教师
     * @param queryTeacher 查询条件
     * @param page 当前页
     * @param size 页面大小
     * @return
     */
    @ApiOperation(value = "根据条件查询教师")
    @PostMapping("/queryTeacherCondition/{page}/{size}")
    public R queryTeacherByCondition(@RequestBody(required = false) QueryTeacher queryTeacher,
                                     @ApiParam(name = "page", value = "当前页", defaultValue = "1") @PathVariable(value = "page") Long page,
                                     @ApiParam(name = "size", value = "当前页", defaultValue = "3") @PathVariable(value = "size") Long size) {
        String name = queryTeacher.getName();

        Long level = queryTeacher.getLevel();

        String begin = queryTeacher.getBegin();

        String end = queryTeacher.getEnd();

        QueryWrapper<EduTeacher> pageWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(name)) {
            pageWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            pageWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            pageWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            pageWrapper.le("gmt_modified", end);
        }

        //排序
        pageWrapper.orderByDesc("gmt_create");

        Page<EduTeacher> eduTeacherPage = new Page<>(page, size);

        eduTeacherService.page(eduTeacherPage, pageWrapper);

        return R.ok().data("page", eduTeacherPage);
    }
}

